package com.tudouji.project.store.controller;

import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.store.domain.Storehouseroom;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStorehouseroomService;
import com.tudouji.project.store.service.IStoreroomService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库房管理Controller
 *
 * @author tudouji
 * @date 2021-01-04
 */
@RestController
@RequestMapping("/store/storeroom")
public class StoreroomController extends BaseController {
	@Autowired
	private IStoreroomService storeroomService;

	@Autowired
	private IStorehouseroomService storehouseroomService;

	/**
	 * 查询库房管理列表
	 */
	@PreAuthorize("@ss.hasPermi('store:storeroom:list')")
	@GetMapping("/list")
	public TableDataInfo list(Storeroom storeroom) {
		startPage();
		List<Storeroom> list = storeroomService.selectStoreroomList(storeroom);
		return getDataTable(list);
	}

	/**
	 * 导出库房管理列表
	 */
	@PreAuthorize("@ss.hasPermi('store:storeroom:export')")
	@Log(title = "库房管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(Storeroom storeroom) {
		List<Storeroom> list = storeroomService.selectStoreroomList(storeroom);
		ExcelUtil<Storeroom> util = new ExcelUtil<Storeroom>(Storeroom.class);
		return util.exportExcel(list, "storeroom");
	}

	/**
	 * 获取库房管理详细信息
	 */
	@GetMapping(value = "/{guid}")
	public AjaxResult getInfo(@PathVariable("guid") String guid) {
		Storeroom storeroom = this.storeroomService.selectStoreroomByGuid(guid);
		Storehouseroom storehouseroom = new Storehouseroom();
		storehouseroom.setStoreguid(guid);
		Integer count = this.storehouseroomService.checkDeleteStore(storehouseroom);
		if (count > 0) {
			storeroom.setWhinfoData(true);
		} else {
			storeroom.setWhinfoData(false);
		}
		return AjaxResult.success(storeroom);
	}

	/**
	 * 新增库房管理
	 */
	@PreAuthorize("@ss.hasPermi('store:storeroom:add')")
	@Log(title = "库房管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Storeroom storeroom) {
		return toAjax(storeroomService.insertStoreroom(storeroom));
	}

	/**
	 * 修改库房管理
	 */
	@PreAuthorize("@ss.hasPermi('store:storeroom:edit')")
	@Log(title = "库房管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Storeroom storeroom) {
//		if (storeroom.isWhinfoData()) {
//			Storeroom store = this.storeroomService.selectStoreroomById(storeroom.getGuid());
//			if (!storeroom.getOrgguid().equals(store.getOrgguid())) {
//				return AjaxResult.error(store.getName() + "有库存数据，不允许修改组织信息");
//			}
//			if (!storeroom.getNature().equals(store.getNature())) {
//				return AjaxResult.error(store.getName() + "有库存数据，不允许修改库房性质");
//			}
//		}
		return toAjax(storeroomService.updateStoreroom(storeroom));
	}

	/**
	 * 删除库房管理
	 */
	@PreAuthorize("@ss.hasPermi('store:storeroom:remove')")
	@Log(title = "库房管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
	public AjaxResult remove(@PathVariable String[] guids) {
		Integer flag = 0;
		String storename = null;
		for (String guid : guids) {
			Storehouseroom storehouseroom = new Storehouseroom();
			storehouseroom.setStoreguid(guid);
			List<Storehouseroom> storehouseroomList = this.storehouseroomService
					.selectStorehouseroomList(storehouseroom);
			if (storehouseroomList.size() > 0) {
				storename = storehouseroomList.get(0).getStoreroom().getName();
				flag = 1;
				break;
			}

			Storeroom storeroom = this.storeroomService.selectStoreroomByGuid(guid);
			Integer count = this.storehouseroomService.checkDeleteStore(storehouseroom);
			if (count > 0) {
				flag = 2;
				storename = storeroom.getName();
				break;
			}
		}

		if (flag == 1) {
			return AjaxResult.error(storename + "中存在库间，不允许删除");
		} else if (flag == 2) {
			return AjaxResult.error(storename + "有库存数据，不允许删除");
		}
		return toAjax(storeroomService.deleteStoreroomByIds(guids));
	}

	/**
	 * 查询库房用于筛选
	 */
	@GetMapping("/findStoreroom")
	public AjaxResult findStoreroom(Storeroom storeroom) {
		List<Storeroom> list = storeroomService.selectStoreroomList(storeroom);
		return AjaxResult.success(list);
	}

	/**
	 * 查询库房用于筛选
	 */
	@GetMapping("/findStoreroomListByPermiss")
	public AjaxResult findStoreroomListByPermiss(Storeroom storeroom) {
		String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
		List<Storeroom> list = null;
		try {
			list = storeroomService.selectStoreroomListByPermiss(storeroom, tuserId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return AjaxResult.success(list);
	}

	/**
	 * 查询库房用于筛选
	 */
	@GetMapping("/findSRByPermiss")
	public AjaxResult findSRByPermiss(Integer permiss) {
		List<Storeroom> list =  storeroomService.findSRByPermiss(permiss);
		return AjaxResult.success(list);
	}

	/**
	 * 查询库房用于筛选
	 */
	@GetMapping("/getStoreroom")
	public AjaxResult getStoreroom(String orgguids) {
		Storeroom storeroom = new Storeroom();
		if (StringUtils.isNotBlank(orgguids)){
			String[] orgs = orgguids.split(",");
			storeroom.setOrgguids(orgs);
		}
		storeroom.setEnable(1);
		List<Storeroom> list =  storeroomService.selectStoreroomList(storeroom);
		return AjaxResult.success(list);
	}
}
